#!/bin/env python
# -*- coding: utf-8 -*-



## ------------------------------------------------------------
## Copyright 2010 Pierre-Antoine Delsart
## 
## This file is part of LeVin.
## 
## LeVin is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## LeVin is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with LeVin.  If not, see <http://www.gnu.org/licenses/>.
## ---------------------------------------------------------------


import sys
import os, os.path
APPDIR = os.path.dirname( os.path.abspath(sys.argv[0]) )
sys.path.append( APPDIR )


from optparse import OptionParser
parser = OptionParser()
parser.add_option('-d','--debug',action="store_true",dest='debug')
parser.add_option('-i','--interactive',action="store_true",dest='interactive')
parser.add_option('--dbname',dest='dbfilename',action='store')
options,args = parser.parse_args( sys.argv)

import qtcommon
join = os.path.join
qtcommon.ICONDIR = join(APPDIR , 'icons' )
qtcommon.DATADIR = join(APPDIR , 'data' )

#qtcommon.setupTranslation()


import wineBase

import wineAreas

import styles


def setupApp(APPDIR):
    join = os.path.join

    ## qtcommon.ICONDIR = join(APPDIR , 'icons' )
    ## qtcommon.DATADIR = join(APPDIR , 'data' )

    qtcommon.setupMainWindow()
    #qtcommon.setupTranslation()

    wineAreas.setupWineColors()
    qtcommon.USERCONFDIR = join(os.path.expanduser('~'),'.config','LeVin')
    confdir = qtcommon.USERCONFDIR
    if not os.path.exists(confdir):
        os.mkdir(confdir)

    if not options.debug:
        dbfile = join( confdir, 'wineDB.sqlite3')
    else:
        if options.dbfilename :
            dbfile = options.dbfilename
            print 'custome file ',dbfile            
        else:
            dbfile = join( confdir, 'testwineDB.sqlite3')
        #dbfile = join( confdir, 'wineDB.sqlite3')
    wineBase.openDB(dbfile)

    areasfile = join(confdir, 'areas.pck' )
    if os.path.exists(areasfile):
        wineAreas.load_wineWorld(areasfile)
    else:
        wineAreas.default_wineWorld()
    import atexit
    atexit.register( wineAreas.save_wineWorld, areasfile)

    styles.Style.substitute
    qtcommon.app.setStyleSheet(styles.Style.substitute(
        ICONDIR=qtcommon.ICONDIR+os.path.sep,
        DATADIR=qtcommon.DATADIR+os.path.sep
        ),                               
                               )


def setupInteractive():
    import os.path
    import readline
    join = os.path.join
    #histfile = join(qtcommon.USERCONFDIR, 'history')
    ## try:
    ##     readline.read_history_file(histfile)
    ## except IOError:
    ##     pass
    import atexit
    import readline
    readline.set_history_length(500)
    #atexit.register(readline.write_history_file, histfile)
    del os#, histfile
    #rlcompleter2.setup()
    readline.parse_and_bind("tab: complete")
    readline.parse_and_bind("set show-all-if-ambiguous off")
    

setupApp(APPDIR)

print qtcommon.window

import mainWindow as gui

def bla(a):
    print 'blabla',a



from PythonQt import QtGui, QtCore, UsePyside


ui = gui.Ui_MainWindow()





ui.setupUi(qtcommon.window)

print qtcommon.window
if UsePyside:
    ctrl_q = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_Q), qtcommon.window , "","", QtCore.Qt.WindowShortcut )
else:
    ctrl_q = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_Q), qtcommon.window)
QtCore.QObject.connect(ctrl_q, QtCore.SIGNAL("activated()"), qtcommon.app.exit)


qtcommon.window.show()




ui.resizeColumns()
styles.alignLabels( ui )
styles.treeWidgetStyles( ui )

if options.interactive:
    setupInteractive()
    
if not options.debug and not options.interactive:
    res = qtcommon.app.exec_()
    sys.exit(res)
## elif UsePyside :
##     print 'Pyside exec app'
##     qtcommon.app.exec_()
    
import wineAreas2
